package com.css.fxfzaqbz.modules.data.controller;

import com.css.fxfzaqbz.base.response.RestResponse;
import com.css.fxfzaqbz.modules.data.entity.ActiveFaultData;
import com.css.fxfzaqbz.modules.data.queryParams.ActiveFaultDataQueryParams;
import com.css.fxfzaqbz.modules.data.service.ActiveFaultDataService;
import com.css.fxfzaqbz.util.FileUtil2;
import com.css.fxfzaqbz.util.PlatformPageUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 3.3.1构造环境数据管理子模块 -表1活动断层数据表单
 */
@RestController
@RequestMapping("/fxfzaqbz/data")
@Slf4j
public class ActiveFaultDataController {
    @Resource
    ActiveFaultDataService activeFaultDataService ;

    /**
     * 新增activeFaultData
     * @param activeFaultData
     * @return
     */
    @PostMapping("/saveActiveFaultData")
    public RestResponse saveActiveFaultData (@RequestBody ActiveFaultData activeFaultData){
        RestResponse restResponse = null ;
        try{
            restResponse = RestResponse.succeed(activeFaultDataService.saveActiveFaultData(activeFaultData));
        }catch (Exception e){
            log.error("新增活动断层数据失败",e);
            restResponse = RestResponse.fail("新增活动断层数据失败!");
        }
        return restResponse;
    }

    /**
     * 全查询  模糊查询未设定条件
     * @param request
     * @param queryParams
     * @return
     */
    @GetMapping("/getAllActiveFaultData")
    public RestResponse getAllActiveFaultData(HttpServletRequest request, ActiveFaultDataQueryParams queryParams){

        RestResponse restResponse = null;
        try{
            int curPage = Integer.valueOf(request.getParameter(PlatformPageUtils.CUR_PAGE));
            int pageSize = Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            queryParams.setCurPage(curPage);
            queryParams.setPageSize(pageSize);
            restResponse = RestResponse.succeed(activeFaultDataService.getAllActiveFaultData(queryParams));
        }catch (Exception e){
            log.error("查询活动断层数据失败!",e);
            restResponse = RestResponse.fail("查询活动断层数据失败");
        }
        return restResponse;
    }

    /**
     * 根据id查询elevation数据
     * @param id
     * @return
     */
    @GetMapping("/getActiveFaultData/{id}")
    public RestResponse<ActiveFaultData> getUser(@PathVariable("id") String id) {
        RestResponse restResponse = null;
        try{
            restResponse = RestResponse.succeed(activeFaultDataService.getActiveFaultDataById(id));
        }catch (Exception e){
            log.error("获取活动断层数据失败!",e);
            restResponse = RestResponse.fail("获取活动断层数据失败!");
        }
        return restResponse;
    }


    /**
     * 逻辑删除，修改数据标志位
     * @param ids
     * @return
     */
    @PostMapping("/delActiveFaultData/{ids}")
    public RestResponse deleteActiveFaultData(@PathVariable("ids") List<String> ids) {
        RestResponse restResponse = null;
        try{
            activeFaultDataService.deleteActiveFaultDatas(ids);
            restResponse = RestResponse.succeed();
        }catch (Exception e){
            log.error("删除活动断层数据失败!",e);
            restResponse = RestResponse.fail("删除活动断层数据失败!");
        }
        return restResponse;
    }

    /**
     * 修改
     * @param activeFaultData
     * @return
     */
    @PostMapping("/updateActiveFaultData")
    public RestResponse<ActiveFaultData> updateActiveFaultData(@RequestBody ActiveFaultData activeFaultData)  {
        RestResponse restResponse = null;
        try{
            restResponse = RestResponse.succeed(activeFaultDataService.updateActiveFaultData(activeFaultData));
        }catch (Exception e){
            log.error("修改活动断层数据失败!",e);
            restResponse = RestResponse.fail("修改活动断层数据失败!");
        }
        return restResponse;
    }

    /**
     * 导出表内数据
     * @param response
     * @throws IOException
     */
    @PostMapping("/exportExcelActiveFaultData")
    public void exportExcel(HttpServletResponse response ) throws IOException {
        RestResponse restResponse = null;
        try{
            List<ActiveFaultData> list  = activeFaultDataService.getAll();
            FileUtil2.exportExcel(list, "活动断层数据", "sheet", ActiveFaultData.class, "ActiveFaultData", response);
            restResponse = RestResponse.succeed("导出成功");
        }catch (Exception e){
            log.error("导出失败",e);
            restResponse = RestResponse.fail("导出失败");
        }
    }

    /**
     * 导入数据
     * @param file
     * @throws IOException
     */
    @PostMapping("/importExcelActiveFaultData")
    public RestResponse importExcel(MultipartFile file) throws IOException {
        RestResponse restResponse = null;
        try{
            activeFaultDataService.importExcel(file);
            restResponse = RestResponse.succeed("导入成功");
        }catch (Exception e){
            log.error("导入失败",e);

        }
        return restResponse ;
    }
}
